﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZOVNP.Model.Abstracts;

namespace ZOVNP.WinView
{
    /// <summary> Форма смены пароля </summary>
    public sealed class ChangePasswordForm : Form
    {
        /// <summary> Кнопка ОК </summary>
        private Button m_btnOK;
        /// <summary> Старый пароль </summary>
        private TextBox m_OldPassword;
        /// <summary> Новый пароль </summary>
        private TextBox m_NewPassword1;
        /// <summary> Новый пароль </summary>
        private TextBox m_NewPassword2;
        private Login m_login;

        /// <summary> Конструктор </summary>
        /// <param name="login"> логин </param>
        public ChangePasswordForm(Login login)
        {
            m_login = login;

            base.SuspendLayout();
            base.Font = SystemFonts.IconTitleFont;
            TableLayoutPanel tblLayoutPanel = new TableLayoutPanel();
            tblLayoutPanel.SuspendLayout();
            tblLayoutPanel.Padding = new Padding(10);
            tblLayoutPanel.Dock = DockStyle.Fill;
            tblLayoutPanel.ColumnCount = 3;
            tblLayoutPanel.RowCount = 4;
            //tblLayoutPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;

            tblLayoutPanel.AutoSize = true;
            // m_btnOK
            m_btnOK = new Button();
            //m_btnOK.DialogResult = DialogResult.OK;
            m_btnOK.FlatStyle = FlatStyle.System;
            m_btnOK.TabIndex = 1;
            m_btnOK.Text = "OK";
            //m_btnOK.Enabled = false;
            m_btnOK.Click += OnOKClick;
            m_btnOK.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

            // m_btnCancel
            Button btnCancel = new Button();
            btnCancel.DialogResult = DialogResult.Cancel;
            btnCancel.FlatStyle = FlatStyle.System;
            btnCancel.TabIndex = 2;
            btnCancel.Text = "Отмена";
            btnCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;


            tblLayoutPanel.Controls.Add(CreateLabel("Старый пароль : "), 0, 0);
            tblLayoutPanel.Controls.Add(CreateLabel("Новый пароль : "), 0, 1);
            tblLayoutPanel.Controls.Add(CreateLabel("Подтверждение : "), 0, 2);

            tblLayoutPanel.Controls.Add(m_btnOK, 1, 3);
            tblLayoutPanel.Controls.Add(btnCancel, 2, 3);

            m_OldPassword = CreateTextBox();
            if (!String.IsNullOrEmpty(m_login.Password))
                m_OldPassword.Text = m_login.Password;

            m_NewPassword1 = CreateTextBox();
            m_NewPassword2 = CreateTextBox();

            tblLayoutPanel.Controls.Add(m_OldPassword, 1, 0);
            tblLayoutPanel.SetColumnSpan(m_OldPassword, 2);
            tblLayoutPanel.Controls.Add(m_NewPassword1, 1, 1);
            tblLayoutPanel.SetColumnSpan(m_NewPassword1, 2);
            tblLayoutPanel.Controls.Add(m_NewPassword2, 1, 2);
            tblLayoutPanel.SetColumnSpan(m_NewPassword2, 2);

            base.ShowIcon = false;
            //base.AutoScaleDimensions = new SizeF(6F, 13F);
            base.AcceptButton = m_btnOK;
            base.CancelButton = btnCancel;
            //base.AutoScaleMode = AutoScaleMode.Font;
            //base.ClientSize = new Size(300, 200);
            base.Controls.Add(tblLayoutPanel);
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "formChangePassword";
            base.StartPosition = FormStartPosition.CenterScreen;
            base.Text = "Смена пароля";

            tblLayoutPanel.ResumeLayout(false);
            tblLayoutPanel.PerformLayout();
            base.ResumeLayout(false);
        }
        /// <summary> Создать Label </summary>
        /// <param name="text"> текст </param>
        /// <returns> новый экземпляр Label</returns>
        private static Label CreateLabel(string text)
        {
            Label result = new Label();
            result.Text = text;
            result.TextAlign = ContentAlignment.MiddleLeft;
            result.Dock = DockStyle.Fill;
            return (result);
        }
        /// <summary> Создать TextBox </summary>
        /// <returns> новый экземпляр TextBox </returns>
        private static TextBox CreateTextBox()
        {
            TextBox result = new TextBox();
            result.Dock = DockStyle.Fill;
            result.PasswordChar = '*';
            return (result);
        }
        /// <summary> Обработчик нажатия кнопки OK </summary>
        /// <param name="sender"> источник события </param>
        /// <param name="e"> параметры события </param>
        private void OnOKClick(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                m_login.PasswordNew = m_NewPassword1.Text;
                base.DialogResult = DialogResult.OK;
                //Close();
            }
        }
        /// <summary> Проверка пользовательского </summary>
        /// <returns> true, если ввод корректен, в противном случае - false </returns>
        private bool CheckInput()
        {
            bool result = (m_NewPassword1.Text == m_NewPassword2.Text);
            if (!result)
                new ServiceLocatorHelper().ServiceLocator.GetService<IMessageService>().ShowMessage("Пароли не совпадают");

            return (result);
        }
    }
}
